package com.aco.fatureapp.utilitary;

import java.text.NumberFormat;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.aco.fatureapp.R;

/**
 *
 *
 */
public class GpsUtils implements LocationListener {
	Context context;

	ProgressDialog progressDialog;

	protected LocationManager locationManager;

	protected Location location;

	protected double latitude;

	protected double longitude;

	public GpsUtils(final Context context) {
		this.context = context;
	}

	/**
	 *
	 * Verifica se o GPS está ativo.
	 *
	 * @return
	 */
	public Boolean verifyIsProviderEnabled() {
		return verificarGpsHabilitado(context);
	}

	/**
	 * Verificar se o Gps está habilitado, caso não esteja chamar a tela padrão
	 * para habilitar.
	 *
	 * @return Verdadeiro caso o GPS esteja habilitado.
	 */
	public static Boolean verificarGpsHabilitado(final Context context) {
		final LocationManager locationManager = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);

		// Verifica se o GPS está ativo.
		if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
			return Boolean.TRUE;
		}

		Utils.alert(context, "O GPS está desligado, deseja ativá-lo agora?",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(final DialogInterface dialog,
							final int which) {
						/*
						 * Call intent gps settings.
						 */
						final Intent intent = new Intent(
								android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);

						context.startActivity(intent);
					}
				}, null);

		return Boolean.FALSE;
	}

	/**
	 *
	 * Inicia o monitoramento da posição.
	 *
	 * @param locationManager
	 */
	public void startLocation(final LocationManager locationManager) {
		this.locationManager = locationManager;

		// Inicia o monitoramento do GPS
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
				0, (LocationListener) context);

		// Exibe o progress dialog
		progressDialog = ProgressDialog.show(context, "GPS", context
				.getResources().getString(R.string.msg_loading_location),
				false, true);
	}

	/**
	 * Inicia o monitoramento da posição, atualizando a location em mais ou
	 * menos 1(um) minuto.
	 */
	public void startLocation() {
		this.locationManager = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);

		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
				900 * 1000, 5000, this);
	}

	/**
	 *
	 * @param locationManager
	 */
	public void stopLocation(final LocationManager locationManager) {
		// Fecha o progress dialog
		progressDialog.dismiss();

		// Para o monitoramento do GPS
		locationManager.removeUpdates(this);
	}

	/**
	 *
	 */
	public void stopLocation() {
		// Para o monitoramento do GPS
		locationManager.removeUpdates(this);
	}

	/**
	 *
	 */
	public void pararLocalizacao() {
		if (locationManager != null) {
			// Para o monitoramento do GPS
			locationManager.removeUpdates(this);
		}
	}

	/**
	 *
	 * @param latLng
	 * @return
	 */
	public static String formatLatLng(double latLng) {
		final NumberFormat nf = NumberFormat.getInstance();
		nf.setMaximumFractionDigits(8);
		nf.setGroupingUsed(false);

		return nf.format(latLng);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * android.location.LocationListener#onLocationChanged(android.location.
	 * Location )
	 */
	@Override
	public void onLocationChanged(Location location) {
		this.location = location;
		this.longitude = location.getLongitude();
		this.latitude = location.getLatitude();
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see android.location.LocationListener#onStatusChanged(java.lang.String,
	 * int, android.os.Bundle)
	 */
	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {

	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * android.location.LocationListener#onProviderEnabled(java.lang.String)
	 */
	@Override
	public void onProviderEnabled(String provider) {

	}

	@Override
	public void onProviderDisabled(String provider) {

	}

	/**
	 *
	 * @return
	 */
	public double getLatitude() {
		return latitude;
	}

	/**
	 *
	 * @return
	 */
	public double getLongitude() {
		return longitude;
	}

}